package com.wl.dzt.basic.controller;

import com.wl.dzt.basic.ApplicationContext;
import com.wl.dzt.basic.entity.BasicDicColorRender;
import com.wl.dzt.basic.entity.BasicUser;
import com.wl.dzt.basic.entity.BasicVtype;
import com.wl.dzt.basic.service.BasicService;
import com.wl.dzt.basic.service.callback.SmsSendSwitchCallback;
import com.wl.dzt.basic.user.User;
import com.wl.dzt.basic.user.UserBuilder;
import com.wl.dzt.sys.constant.CommConstant;
import com.wl.dzt.sys.controller.result.FormResult;
import com.wl.dzt.sys.core.dao.DB;
import com.wl.dzt.sys.core.dao.JDao;
import com.wl.dzt.sys.core.dao.select.Select;
import com.wl.dzt.sys.response.Result;
import com.wl.dzt.sys.ui.ext.ExtTemplate;
import com.wl.dzt.sys.util.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/6/27/027
 * @description 用户初始化
 */
@RestController
@RequestMapping("admin")
@ApiIgnore
public class BasicAdminController {


    @Resource
    private JDao jDao;

    @Resource
    private BasicService basicService;



    /**
     * <b>重置密码</b>
     * <p>
     * 重置后密码为 888888
     * </p>
     *
     * @param request
     * @param id
     */
    @ApiOperation("重置密码")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "用户编号", dataType = "String", required = true, paramType = "body")
    )
    @PostMapping("resetPwd")
    public void resetPwd(HttpServletRequest request, HttpServletResponse response, String id) {
        BasicUser user = DB.get(BasicUser.class, id, "organId.id");
        user.setPwd(PwdUtils.MD5("888888"));
        DB.save(user);
        SmsSendSwitchCallback.handler(user.getOrganId().getId(), () -> AliyunSmsUtils.send(user.getPhone(), "SMS_139227797", new DynaBean().set("name", user.getName()).set("uname", user.getUname()).set("pwd", "888888")));
        WebUtils.print("true", response);
    }



    /**
     * <b>退出系统</b>
     * <p>
     * 过期掉session
     * </p>
     *
     * @param request
     */
    @RequestMapping("extiSystem")
    public void extiSystem(HttpServletRequest request) {
        request.getSession().invalidate();
    }

    /**
     * <b>改变密码</b>
     * <p>
     * 原来密码输入一致<br>
     * 二次新密码输入一致则可以修改
     * </p>
     *
     * @param request
     * @param response
     */
    @RequestMapping("changePwd")
    public void changePwd(HttpServletRequest request, HttpServletResponse response, BasicUser form, String newPwd) {
        User user = WebUtils.getUser(request);
        BasicUser basicUser = (BasicUser) user.getSystemUser();
        FormResult formResult = new FormResult();
        formResult.setSuccess(true);
        if (!basicUser.getPwd().equalsIgnoreCase(DigestUtils.md5Hex(form.getPwd()))) {
            formResult.setField("pwd");
            formResult.setSuccess(false);
            Map<String, String> map = new HashMap<>();
            map.put("pwd", "原密码不正确");
            formResult.setErrors(map);
        } else {
            basicUser.setPwd(DigestUtils.md5Hex(newPwd));
            DB.save(basicUser);
        }
        ExtTemplate.formTemplate(formResult, response);
    }




    /**
     * <b>页面初始化</b>
     * <p>
     * 加载验证器<br>
     * 加载颜色渲染器<br>
     * 加载当前登陆人<br>
     * 加载配置项<br>
     * 加载字典<br>
     * 加载模块<br>
     * </p>
     *
     * @param request
     * @param response
     */
    @PostMapping("init")
    public void init(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        // 验证
        {
            List<BasicVtype> list = jDao.list(Select.create(BasicVtype.class));
            map.put("vtypes", list);
        }

        // 颜色渲染器
        {
            List<BasicDicColorRender> list = jDao.list(Select.create(BasicDicColorRender.class));
            map.put("colors", list);
        }

        User user = WebUtils.getUser(request);
        // 当前登陆人和配置项
        {
            map.put("user", user.getUserInof());
            map.put("cnf", user.getConfig());
        }

        // 模块
        {
            map.put("modeList", basicService.modelList(user));
        }

        // 字典
        {
            map.put("dics", basicService.dicList());
        }
        WebUtils.json(map, response);
    }


}
